package com.example.lenovo.downloaddemo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lenovo.downloaddemo.R;
import com.example.lenovo.downloaddemo.bean.CategoryBean;
import com.example.lenovo.downloaddemo.bean.RankBean;
import com.example.lenovo.downloaddemo.http.AppConfig;
import com.example.lenovo.downloaddemo.http.okhttp.utils.AppContext;
import com.example.lenovo.downloaddemo.http.okhttp.utils.CommonUtils;
import com.example.lenovo.downloaddemo.util.TextStyleLanguage;
import com.squareup.picasso.Picasso;

import java.util.List;

public class CategoryAdapter extends BaseAdapter {
    private Context context;

    private List<CategoryBean.DataBean.CategoryListBean> data;
    public CategoryAdapter(Context context,List<CategoryBean.DataBean.CategoryListBean> data){
        this.context=context;
        this.data=data;

    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHoler holer=null;
        if(view==null){
            holer=new ViewHoler();
            view=LayoutInflater.from(AppContext.getInstance()).inflate(R.layout.category_item,null);
            holer.tv_name=(TextView) view.findViewById(R.id.tv_category_name);
            holer.iv_Cate=(ImageView) view.findViewById(R.id.iv_cate);
            view.setTag(holer);

        }else{
            holer= (ViewHoler) view.getTag();
        }
        Log.e("====",data.get(i).getName());
        if(!TextStyleLanguage.isEngLish()){
            holer.tv_name.setText(data.get(i).getName());
        }else{
            holer.tv_name.setText(data.get(i).getName_en());
        }

        int width = (CommonUtils.getScreenWidth(context))/5;
        int height=CommonUtils.getScreenHeight(context)/25;
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, height);

        holer.iv_Cate.setLayoutParams(lp);
        Picasso.with(context)
                .load(AppConfig.IP+data.get(i).getImg())
                .resize(width,height)
                .config(Bitmap.Config.RGB_565)
                .into(holer.iv_Cate);
        return view;
    }
    static class ViewHoler{
        TextView tv_name;
        ImageView iv_Cate;
    }
}
